<template>
    <MenuPage 
        ref="menuPage"
        :title="title"
        :columns="columns" 
        :data="data"
        :total="total"
        :page.sync="query.pageIndex"
        :size.sync="query.pageSize"
        :query="query"
        @add="showEditModal"
        @queryFun="queryList">
        <!-- 创建 / 修改 -->
        <EditModal
            ref="editModal"
            :title="title"
            :projectId="projectId"
            @ok="handleOk"
        ></EditModal>
    </MenuPage>
</template>
<script>
import EditModal from './edit.vue';
import { mapGetters, mapState, mapMutations } from 'vuex';
import { queryGitProjectBranch, delGitProjectBranch } from '../../api/gitManage.js';
import { gitDownloadMixin } from '../mixin';
// import pageTitle from '@/mixin/pageTitle';
export default {
    name: 'Branch',
    props: {},
    components: { EditModal },
    mixins: [ gitDownloadMixin ],
    data() {
        return {
            title:'代码分支',
            projectId: this.$route.query.id,
            query: {
                pageIndex: 1,
                pageSize: 10,
            },
            columns: [
                {
                    title: '名称',
                    key: 'name',
                    placement: 'top-left',
                    width: 200,
                    tooltip: true,
                    queryForm:{
                        component:'Input'
                    }
                },
                {
                    title: '创建者',
                    key: 'authorName',
                    width: 100,
                    tooltip: true,
                },
                {
                    title: '最近修改描述',
                    key: 'message',
                    width: 100,
                    tooltip: true,
                },
                {
                    title: '最近修改人员',
                    key: 'committerName',
                    width: 100,
                    tooltip: true,
                },
                {
                    title: '最后修改时间',
                    key: 'committedDate',
                    width: 140,
                    tooltip: true,
                },
                {
                    title: '操作',
                    placement: 'top-right',
                    width:120,
                    key:'action',
                    opList:[
                        {
                            txt:'下载',
                            disabled:(data) => this.userName != data.authorName,
                            fun:(data) => {
                                this.gitDownload(data.id, {
                                    name: this.$route.query.name,
                                    branch: data.name,
                                });
                            }
                        },
                        {
                            txt:'删除',
                            type:'delete',
                            tooltip:true,
                            disabled: (data) => this.userName != data.authorName,
                            title: '你确定要删除吗?',
                            fun:this.delItem
                        }
                    ],
                },
            ],
            data: [],
            total: 0
        };
    },
    computed: {
        ...mapState('user', ['userId', 'userName', 'isRootDesigner']),
    },
    watch: {
    
    },
    mounted() {
        this.addBreadItem({ name: this.$route.query.name });
        this.queryList();
    },
    methods: {
        ...mapMutations(['addBreadItem']),
        queryList() {
            let query = { ...this.query };
            query.projectId = this.projectId;
            queryGitProjectBranch(query).then((res) => {
                if (res.code == 1 && res.data) {
                    const data = res.data.items;
                    this.data = data.map((item) => {
                        return { ...item, ...item.commit };
                    })
                    this.total = res.data.total;;
                } else {
                    this.tdata = [];;
                    this.total = 0;
                }
            });
        },
        refresh() {
            this.query = { pageIndex: 1, pageSize: this.query.pageSize };
            this.search();
        },
        handleOk(type) {
            type === 'add' ?  this.refresh() : this.queryList();
        },
        delItem(params) {
            delGitProjectBranch(this.projectId, params.name).then((res) => {
                if (res.code == 1) {
                    this.$Message.info('删除成功!');
                    if (
                        this.data.length == 1 &&
                        this.data.pageIndex > 1
                    ) {
                        this.data.pageIndex--;
                    }
                    this.queryList();
                } else {
                    this.$Message.error(res.message);
                }
            });
        },
        showEditModal(data) {
            this.$refs.editModal.showModal(data);
        },
    },
    beforeDestroy() {},
};
</script>

<style scoped>
</style>
